package com.ruoyi.messages.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.messages.domain.SysPublishStatusDTO;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.messages.mapper.SysPublishMapper;
import com.ruoyi.messages.domain.SysPublish;
import com.ruoyi.messages.service.ISysPublishService;

/**
 * 公告管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-14
 */


@Service
public class SysPublishServiceImpl implements ISysPublishService
{
    @Autowired
    private SysPublishMapper sysPublishMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查询公告管理
     *
     * @param id 公告管理主键
     * @return 公告管理
     */
    @Override
    public SysPublish selectSysPublishById(Long id)
    {
        return sysPublishMapper.selectSysPublishById(id);
    }

    /**
     * 查询公告管理列表
     *
     * @param sysPublish 公告管理
     * @return 公告管理
     */
    @Override
    public List<SysPublish> selectSysPublishList(SysPublish sysPublish)
    {

        sysPublish.setDeptId(SecurityUtils.getDeptId());
        return sysPublishMapper.selectSysPublishList(sysPublish);
    }

    /**
     * 新增公告管理
     *
     * @param sysPublish 公告管理
     * @return 结果
     */
    @Override
    public int insertSysPublish(SysPublish sysPublish)
    {
        sysPublish.setCreateTime(DateUtils.getNowDate());
        sysPublish.setUserId(SecurityUtils.getUserId());
        Long deptId = SecurityUtils.getDeptId();
        sysPublish.setDeptId(deptId);
        String type = deptMapper.selectDeptById(deptId).getType();

        if (type.equals("0")) {
            //管理端,所有人都可见
            sysPublish.setType("0");
        }
        else if (type.equals("1")) {
            //自己代理商用户和自己商铺用户可见
            sysPublish.setType("3");
        }


        return sysPublishMapper.insertSysPublish(sysPublish);
    }

    /**
     * 修改公告管理
     *
     * @param sysPublish 公告管理
     * @return 结果
     */
    @Override
    public int updateSysPublish(SysPublish sysPublish)
    {
        sysPublish.setUpdateTime(DateUtils.getNowDate());
        return sysPublishMapper.updateSysPublish(sysPublish);
    }

    /**
     * 批量删除公告管理
     *
     * @param ids 需要删除的公告管理主键
     * @return 结果
     */
    @Override
    public int deleteSysPublishByIds(Long[] ids)
    {
        return sysPublishMapper.deleteSysPublishByIds(ids);
    }

    /**
     * 删除公告管理信息
     *
     * @param id 公告管理主键
     * @return 结果
     */
    @Override
    public int deleteSysPublishById(Long id)
    {
        return sysPublishMapper.deleteSysPublishById(id);
    }

    @Override
    public int updateStatus(SysPublishStatusDTO statusDTO) {

        SysPublish sysPublish = new SysPublish();
        sysPublish.setId(statusDTO.getId());
        sysPublish.setStatus(statusDTO.getStatus());


        return  sysPublishMapper.updateSysPublish(sysPublish);
    }
}
